import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AlarmClockSetting extends javax.swing.JPanel {
	
	JPanel mainPanel;
	String hourChoice = null, minuteChoice = null, ampmChoice = null;
	String hour_ = null, minute_ = null, ampm = null;
	
    public AlarmClockSetting(JPanel panel) {
    	mainPanel = panel;
        initComponents();
        cancel.addActionListener(new PanelSwitchToCalendar());
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        save = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        hourBox = new javax.swing.JComboBox();
        hour = new javax.swing.JLabel();
        minute = new javax.swing.JLabel();
        minuteBox = new javax.swing.JComboBox();
        ampmBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();


        save.setText("SAVE");
        save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });

        cancel.setText("BACK");

        //add the data into the the hour combo box
        hourBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"}));
        hourBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hourBoxActionPerformed(evt);
            }
        });

        hour.setText("Hour :");

        minute.setText("Minute :");

        minuteBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "00", "05", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55" }));
        minuteBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                minuteBoxActionPerformed(evt);
            }
        });

        ampmBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] {"AM", "PM" }));
        ampmBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ampmBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(52, Short.MAX_VALUE)
                .addComponent(hour)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(hourBox, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47)
                .addComponent(minute)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(minuteBox, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(74, 74, 74)
                        .addComponent(ampmBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(save, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(100, 100, 100))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(minute)
                    .addComponent(hourBox)
                    .addComponent(ampmBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(minuteBox)
                    .addComponent(hour))
                .addGap(69, 69, 69)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(save, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cancel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Alarm Clock");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(clock);
        clock.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 377, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 342, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 650, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(99, 99, 99)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(39, 39, 39))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(143, 143, 143)
                .addComponent(clock, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(130, 130, 130))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(35, 35, 35)
                .addComponent(clock, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36))
        );
    }// </editor-fold>
    
    class PanelSwitchToCalendar implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            CardLayout c = (CardLayout)(mainPanel.getLayout());
            c.show(mainPanel, "Calendar");
        }
    }
    
    //save action perform, pop up menu
    private void saveActionPerformed(java.awt.event.ActionEvent evt) {
		int save = JOptionPane.showConfirmDialog(this,"Are you sure you want to save the changes that you have made? ","Alarm Clock",JOptionPane.YES_NO_OPTION);
        if(save == JOptionPane.YES_NO_OPTION){
            try{
            	try {
        			file.writeAlarm(hourChoice,minuteChoice,ampmChoice);
        		} catch (IOException e) {
        			e.printStackTrace();
        		}
            	JOptionPane.showMessageDialog(this,"Your information have been saved","Alarm Clock",JOptionPane.INFORMATION_MESSAGE);
            }
            catch(Exception e){
                JOptionPane.showMessageDialog(this,"WARNING : "+ e.getMessage(),"Alarm Clock",JOptionPane.WARNING_MESSAGE);
            }
        }
    }                                    
    
    
    private void hourBoxActionPerformed(java.awt.event.ActionEvent evt) {
    	int Selection;
    	hourChoice = (String)hourBox.getSelectedItem();
    	//read the number of hour selected by the user
    	Selection = hourBox.getSelectedIndex();
    	
    	for(int i = 0; i <= Selection; i++)
    		clock.setHour(i);
    }                                       

    private void minuteBoxActionPerformed(java.awt.event.ActionEvent evt) {
    	int Selection;
    	minuteChoice = (String)minuteBox.getSelectedItem();
    	Selection = minuteBox.getSelectedIndex();
    	
    	for(int i = 0; i <= Selection; i++)
    		clock.setMinute(i);
    }                                         

    private void ampmBoxActionPerformed(java.awt.event.ActionEvent evt) {   
    	ampmChoice = (String)ampmBox.getSelectedItem();
    }                                       
    
    
    public StillClock clock = new StillClock();
    public WriteTextFile file = new WriteTextFile();
    private javax.swing.JComboBox ampmBox;
    private javax.swing.JButton cancel;
    private javax.swing.JLabel hour;
    private javax.swing.JComboBox hourBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel minute;
    private javax.swing.JComboBox minuteBox;
    private javax.swing.JButton save;
}

class StillClock extends JPanel{
    private int hour;
    private int minute;


    public StillClock(){}

    public StillClock(int hour, int minute, int second){
        this.hour = hour;
        this.minute = minute;
    }

    public int getHour(){
        return hour;
    }

    public void setHour(int hour){
        this.hour = hour;
        repaint();
    }

    public int getMinute(){
        return minute;
    }

    public void setMinute(int minute){
        this.minute = minute;
        repaint();
    }


    public void paintComponent(Graphics g){
        super.paintComponent(g);

        int clockRadius = (int)(Math.min(getWidth(),getHeight())* 0.8 * 0.5);
        int xCenter = getWidth()/2;
        int yCenter = getHeight()/2;

        g.setColor(Color.white);
        g.fillOval(xCenter - clockRadius, yCenter - clockRadius, 2*clockRadius, 2*clockRadius);
        g.setColor(Color.black);
        g.drawOval(xCenter - clockRadius, yCenter - clockRadius, 2*clockRadius, 2*clockRadius);
        g.drawString("12",xCenter-5, yCenter-clockRadius+12);
        g.drawString("9",xCenter-clockRadius+3, yCenter+5);
        g.drawString("3",xCenter+clockRadius-10, yCenter+3);
        g.drawString("6",xCenter-3, yCenter+clockRadius-3);

        int mLength = (int)(clockRadius*0.65);
        int xMinute = (int)(xCenter+mLength*Math.sin(minute*2*Math.PI/12));
        int yMinute = (int)(yCenter-mLength*Math.cos(minute*(2*Math.PI/12)));
        g.setColor(Color.blue);
        g.drawLine(xCenter, yCenter, xMinute, yMinute);

        int hLength = (int)(clockRadius*0.5);
        int xHour = (int)(xCenter + hLength*Math.sin((hour%12+minute/60.0)*(2*Math.PI/12)));
        int yHour = (int)(yCenter - hLength*Math.cos((hour%12+minute/60.0)*(2*Math.PI/12)));
        g.setColor(Color.red);
        g.drawLine(xCenter, yCenter, xHour, yHour);

    }

    public Dimension getPreferredSize(){
        return new Dimension(200, 200);
    }
}


class WriteTextFile{
    public void writeAlarm(String hourChoice, String minuteChoice, String ampmChoice)throws IOException{
    	Writer output = null;
        
        if(ampmChoice == null) {
        	ampmChoice= "AM";
        	if(hourChoice.equals("12"))
        		hourChoice = "00";
    	}
        else if(ampmChoice.equals("PM"))
        	hourChoice = Integer.toString(Integer.parseInt(hourChoice) + 12);
        
        if(hourChoice == null)
        	hourChoice = "01";
        
        if(minuteChoice == null)
        	minuteChoice = "00";
        
        File file = new File("Alarm.txt");
        output = new BufferedWriter(new FileWriter(file));
        output.write(hourChoice+ ":"+ minuteChoice + ":" + "00");
        output.close();       
        }
}